package com.publiccms.views.directive.tools;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.publiccms.entities.sys.SysSite;
import com.publiccms.logic.service.sys.SysSiteService;
import com.publiccms.common.base.AbstractTemplateDirective;
import com.sanluan.common.handler.RenderHandler;

@Component
public class ResourcePathDirective extends AbstractTemplateDirective {
    @Override
    public void execute(RenderHandler handler) throws IOException, Exception {
        String path = handler.getString("path");
        SysSite site = sysSiteService.getEntity(getSiteId(handler));
        if (notEmpty(site)) {
            if (notEmpty(path)) {
                if (0 <= path.indexOf("://") || 0 == path.indexOf("//")) {
                    handler.print(path);
                } else {
                    handler.print(site.getResourcePath() + path);
                }
            } else {
                handler.print(site.getResourcePath());
            }
        } else {
            handler.print(path);
        }
    }

    @Autowired
    private SysSiteService sysSiteService;
}
